package com.example.fw;

import java.util.HashSet;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class ContactHelper extends HelperWithWebDriverBase {
	
//	private Contacts contactCache;
	private Contacts contactModel;
    private int lastContactId;
	
	public ContactHelper (ApplicationManager manager){
		super(manager);
		lastContactId = Integer.parseInt(
				manager.getHibernateHelper().getLastContactId());
		contactModel = getContacts();
	}
	
	public Contacts getContacts() {
		if ( contactModel == null){
			contactModel = manager.getHibernateHelper().listContacts();
		}
		return contactModel;
	}
	
	public Contacts getUiContacts() {
		HashSet<ContactObject> set = new HashSet<ContactObject>();
		manager.getNavigationHelper().openMainPage();
		WebElement form = findElements(By.tagName("form")).get(2);
		List<WebElement> checkboxes = form.findElements(By.name(manager.getProperty("nameCheckbox")));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute(manager.getProperty("title"));
//			Pattern p = Pattern.compile(".*\\((.*)\\)");
//			Matcher m = p.matcher(title);
//			if (m.matches()){
//				title = m.group(1);
//			}
//			int startIndex = title.indexOf("(");
//			int endIndex = title.lastIndexOf(")");
//			title = title.substring(startIndex, endIndex);
//			title = title.substring("Select (".length(), title.length()-")".length());
			int endIndex = title.lastIndexOf(" ");
			String firstName = title.substring("Select (".length(), endIndex);
			String lastName = title.substring(endIndex+1,title.length()-")".length());
			ContactObject contact = new ContactObject()
				.setFirstName(firstName)
				.setLastName(lastName)
				.setId(checkbox.getAttribute(manager.getProperty("idCheckbox")));
			set.add(contact);
		}
		return new Contacts (set);
	}

	public void createContact(ContactObject contactObject) {
		initContactCreation();
		fillContactForm(contactObject, true);
		submitContactCreation();
		returnToHomePage();
		lastContactId = lastContactId + 1;
		contactObject.setId("" + lastContactId);
		contactModel = contactModel.withAdded(contactObject);
	}

	private void submitContactCreation() {
		click(By.name(manager.getProperty("submitLink")));
	}

	private void returnToHomePage() {
		click(By.linkText(manager.getProperty("HomePageLink")));
	}

	private void fillContactForm(ContactObject contactObject, boolean firstInput) {
		type(By.name("firstname"), contactObject.firstName);
		type(By.name("lastname"), contactObject.lastName);
		type(By.name("address"), contactObject.address);
		type(By.name("home"), contactObject.home);
		type(By.name("mobile"), contactObject.mobile);
		type(By.name("work"), contactObject.work);
		type(By.name("email"), contactObject.email);
		type(By.name("email2"), contactObject.email2);
		type(By.name("mobile"), contactObject.mobile);
		SelectFrom(By.name("bday"), contactObject.bday);
		SelectFrom(By.name("bmonth"), contactObject.bmonth);
		type(By.name("byear"), contactObject.byear);
		HelperForContactCreation(contactObject, firstInput);
		type(By.name("address2"), contactObject.address2);
		type(By.name("phone2"), contactObject.phone2);
	}

	private void HelperForContactCreation(ContactObject contactObject,
			boolean firstInput) {
		if (firstInput) {
			SelectFrom(By.name("new_group"), contactObject.selectgroup);
		}
	}

	private void SelectFrom(By locator, String parametr) {
		new Select(findElement(locator)).selectByVisibleText(parametr);
	}

	private void initContactCreation() {
		click(By.linkText(manager.getProperty("initContactCreationLink")));
	}

	public void deleteContact(boolean type, ContactObject contactObject) {
		manager.getNavigationHelper().openMainPage();
		selectContact(type, contactObject);
		deleteSelectedContact();
		returnToHomePage();
		contactModel = contactModel.withRemoved(contactObject);
	}

	private void deleteSelectedContact() {
		click (By.xpath(manager.getProperty("xpathSelectedContact")));
	}

	public void modifyContact(boolean wayForEditPage, ContactObject contactBefore, ContactObject contactAfter) {
		manager.getNavigationHelper().openMainPage();
		selectContact(wayForEditPage, contactBefore);
		fillContactForm(contactAfter, false);
		submitContactModification();
		returnToHomePage();
		contactModel = contactModel.withRemoved(contactBefore).withAdded(contactAfter);
	}

	private void submitContactModification() {
		click (By.name(manager.getProperty("submitContactModificationLink")));
	}

	public void selectContact(boolean type, ContactObject contact) {
		if ( type ){
			findElement(By.xpath("//a[@href='view.php?id="+contact.id+"']")).click();
			click (By.name(manager.getProperty("modifyContact")));
		}
		else {
			findElement(By.xpath("//a[@href='edit.php?id="+contact.id+"']")).click();
		}
	}
}
